// 装饰者模式（Decorator Pattern）
// 装饰者模式动态地将责任附加到对象上。若要扩展功能，装饰者提供了比继承更有弹性的替代方案。

// 组件接口  
class Component {  
  operation() {  
    throw new Error('子类必须实现 operation 方法');  
  }  
}  
  
// 具体组件  
class ConcreteComponent extends Component {  
  operation() {  
    console.log('ConcreteComponent: Operation');  
  }  
}  
  
// 装饰者抽象类  
class Decorator extends Component {  
  constructor(component) {  
    this.component = component;  
  }  
  
  operation() {  
    this.component.operation();  
  }  
}  
  
// 具体装饰者A  
class ConcreteDecoratorA extends Decorator {  
  operation() {  
    super.operation();  
    this.addedBehavior();  
  }  
  
  addedBehavior() {  
    console.log('ConcreteDecoratorA: Added Behavior');  
  }  
}  
  
// 具体装饰者B  
class ConcreteDecoratorB extends Decorator {  
  operation() {  
    this.addedBehavior();  
    super.operation();  
  }  
  
  addedBehavior() {  
    console.log('ConcreteDecoratorB: Added Behavior');  
  }  
}  
  
// 使用装饰者模式  
const component = new ConcreteComponent();  
const decoratorA = new ConcreteDecoratorA(component);  
const decoratorB = new ConcreteDecoratorB(decoratorA);  
  
decoratorB.operation();  
// 输出:  
// ConcreteDecoratorB: Added Behavior  
// ConcreteDecoratorA: Added Behavior  
// ConcreteComponent: Operation